package cn.stylefeng.guns.modular.fi.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.fi.entity.FiHaTempCarAppStand;
import cn.stylefeng.guns.modular.fi.model.params.FiHaTempCarAppStandParam;
import cn.stylefeng.guns.modular.fi.model.params.FiTempCarAppStandParam;
import cn.stylefeng.guns.modular.fi.service.FiHaTempCarAppStandService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


/**
 * 临时车超额审批标准控制器
 *
 * @author wp
 * @Date 2025-03-10 10:50:55
 */
@Controller
@RequestMapping("/fiHaTempCarAppStand")
public class FiHaTempCarAppStandController extends BaseController {

    private String PREFIX = "/fi/fiHaTempCarAppStand";

    @Autowired
    private FiHaTempCarAppStandService fiHaTempCarAppStandService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private DictService dictService;

    /**
     * 跳转到主页面
     *
     * @author wp
     * @Date 2025-03-10
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/fiHaTempCarAppStand.html";
    }


    /**
     * 新增页面
     *
     * @author wp
     * @Date 2025-03-10
     */
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(null, deliverId, user.getOperateId()));
        model.addAttribute("dictlist", dictService.findByDicttypeCode2("COMPANY_TYPE"));
        model.addAttribute("vehiclemodelList", dictService.findByDicttypeCode2("VEHICLE_MODEL"));
        return PREFIX + "/fiHaTempCarAppStand_add.html";

    }

    /**
     * 编辑页面
     *
     * @author wp
     * @Date 2025-03-10
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(null, deliverId, user.getOperateId()));
        model.addAttribute("dictlist", dictService.findByDicttypeCode2("COMPANY_TYPE"));
        model.addAttribute("vehiclemodelList", dictService.findByDicttypeCode2("VEHICLE_MODEL"));
        return PREFIX + "/fiHaTempCarAppStand_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wp
     * @Date 2025-03-10
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FiHaTempCarAppStandParam fiHaTempCarAppStandParam) {

        User user = userService.getcurrentUser();
        fiHaTempCarAppStandParam.setCreateUser(user.getName());
        fiHaTempCarAppStandParam.setCreateTime(new Date());
        BasicOperate basicOperate = basicOperateService.getById(fiHaTempCarAppStandParam.getOperateId());
        fiHaTempCarAppStandParam.setOperateName(basicOperate.getOperateName());
        int rangeExist = fiHaTempCarAppStandService.isRangeExist(FiHaTempCarAppStandParam.builder()
                .operateId(fiHaTempCarAppStandParam.getOperateId())
                .vehicleModel(fiHaTempCarAppStandParam.getVehicleModel())
                .customerType(fiHaTempCarAppStandParam.getCustomerType())
                .kiloStart(fiHaTempCarAppStandParam.getKiloStart())
                .kiloEnd(fiHaTempCarAppStandParam.getKiloEnd())
                .build());
        if(rangeExist > 0){
            return ResponseData.error("公里区间有重叠，请检查后重试！");
        }
        this.fiHaTempCarAppStandService.add(fiHaTempCarAppStandParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author wp
     * @Date 2025-03-10
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FiHaTempCarAppStandParam fiHaTempCarAppStandParam) {

        User user = userService.getcurrentUser();
        fiHaTempCarAppStandParam.setUpdateUser(user.getName());
        fiHaTempCarAppStandParam.setUpdateTime(new Date());
        this.fiHaTempCarAppStandService.update(fiHaTempCarAppStandParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wp
     * @Date 2025-03-10
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FiHaTempCarAppStandParam fiHaTempCarAppStandParam) {
        this.fiHaTempCarAppStandService.delete(fiHaTempCarAppStandParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wp
     * @Date 2025-03-10
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FiHaTempCarAppStandParam fiHaTempCarAppStandParam) {
        FiHaTempCarAppStand detail = this.fiHaTempCarAppStandService.getById(fiHaTempCarAppStandParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wp
     * @Date 2025-03-10
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FiHaTempCarAppStandParam fiHaTempCarAppStandParam,String condition ) {
        return this.fiHaTempCarAppStandService.findPageBySpec(fiHaTempCarAppStandParam,condition);
    }



    /**
     * 跳转到审批页面
     *
     * @author wp
     * @Date 2025-03-10
     */
    @RequestMapping("/approve")
    public String approveIndex(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/fiHaTempCarAppStandExa.html";
    }

    /**
     * 查询审批列表
     *
     * @author wp
     * @Date 2025-03-10
     */

    @ResponseBody
    @RequestMapping("/approveList")
    public LayuiPageInfo approveList(FiHaTempCarAppStandParam fiHaTempCarAppStandParam,String startTime,String endTime,String operateId,String matchRs,String condition ) {
        String approverUser =
                userService.getcurrentUser().getAccount();
        if(!"admin".equals(approverUser)){
            fiHaTempCarAppStandParam.setApproverUser(approverUser);
        }
        if (ToolUtil.isEmpty(startTime) && ToolUtil.isEmpty(endTime)) {
            startTime = DateUtil.getDate();
            endTime = DateUtil.getDate();
            fiHaTempCarAppStandParam.setStartTime(startTime);
            fiHaTempCarAppStandParam.setEndTime(endTime);
        }
        return this.fiHaTempCarAppStandService.findApproveListPageBySpec(fiHaTempCarAppStandParam,condition);
    }

}


